﻿using SuZhouHouses.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SuZhouHouses.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            string language = Request.QueryString["lan"];
            if (!string.IsNullOrEmpty(language))
            {
                string lanU = language.ToUpper();
                if (lanU == "CN")
                {
                    System.Globalization.CultureInfo englishCulture = new System.Globalization.CultureInfo("zh-CN");
                    Session["Culture"] = englishCulture;
                    HttpCookie cookie = new HttpCookie(SuZhouHouses.Utils.Constants.Language);
                    cookie.Value = "zh-CN";
                    cookie.Expires = DateTime.Now.Add(new TimeSpan(5000, 0, 0));
                    Response.AppendCookie(cookie);
                }
                else if (lanU == "US")
                {
                    System.Globalization.CultureInfo englishCulture = new System.Globalization.CultureInfo("en-US");
                    Session["Culture"] = englishCulture;
                    HttpCookie cookie = new HttpCookie(SuZhouHouses.Utils.Constants.Language);
                    cookie.Value = "en-US";
                    cookie.Expires = DateTime.Now.Add(new TimeSpan(5000, 0, 0));
                    Response.AppendCookie(cookie);
                }
            }

            
            return View();
        }

        public ActionResult ContactUs()
        {
            return View();
        }

        public ActionResult Job()
        {
            return View();
        }

        [HttpPost]
        public bool SendContactEmail(bool isTenant)
        {
            try
            {
                string[] emails = System.Configuration.ConfigurationManager.AppSettings["ManagerEmails"].Split(';');

                string name = Request.Form["tbName"];
                string phone = Request.Form["tbPhone"];
                string email = Request.Form["tbEmail"];
                string subject = Request.Form["tbSubject"];
                string content = Request.Form["tbContent"];

                SuZhouHouses.Utils.MailManager mail = new Utils.MailManager();
                foreach (var em in emails)
                {
                    if (Validator.IsValidEmail(em))
                        mail.To.Add(em);
                }
                mail.Subject = subject ;
                if (isTenant)
                    mail.Subject += "----Sent from Tenant";
                else
                    mail.Subject += "----Sent from Landlord";
                mail.Body = string.Format(@"Name:{0} 
Phone:{1} 
Email:{2} 
Content:{3} ", name, phone, email, content);
                mail.Send();

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
